home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Best of Shareware
/
Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso
/
mac
/
DOS
/
PROGRAMG
/
ULTRA250
/
UW_TUT6.C
< prev
next >
Wrap
Text File
|
1992-11-02
|
15KB
|
388 lines
/****************************************************************************/
/* UW_TUT6.C */
/* */
/* NOTE: THIS FILE IS PUBLIC DOMAIN AND MAY BE MODIFIED AND USED AT WILL */
/* */
/* Now we add a simple menu system to allow for loading and saving of */
/* customer files. We add three functions, file load and file save, using */
/* a simple popup window function with string entry to get a filename... */
/* */
/* Dr. Boyd Gafford */
/* Kevin Huck */
/* EnQue Software */
/* 09/11/92 */
/****************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#ifndef __TURBOC__
#include <sys\types.h>
#endif
#include <sys\stat.h>
#include <time.h>
#include <ctype.h>
#include "uw.h" /* include the necessary headers */
#define MAX_CUST 100
typedef struct cust
{
int status;
int cust_no;
char business[34];
char name[34];
char addr[34];
char city[34];
char state[4];
char zip[10];
char phone[16];
char fax[16];
char date[10];
char memo[34];
char unused[26]; /* round out to 256 bytes */
} CUST;
/*----------------------- global window variables --------------------------*/
WINDOW Desk_wn, Window1;
CUST Customers[MAX_CUST];
char Fname[33];
MENU Top_menu, *Top_mnp = &Top_menu;
MENU Files_menu, Edit_menu;
MENU *Drop_mnps[3];
/*-------------------------------- prototypes ------------------------------*/
int disp_time(void);
void disp_cust(CUST *cp, WINDOW *wnp);
int file_load(CUST *customers);
int file_save(CUST *customers);
int get_fname(char *fname);
/*********/
/* ~main */
/* ********************************************************************/
/* Demonstrate data entry capability... */
/****************************************************************************/
int main()
{
int ret_val, cust = 0, end_flag = 0;
WINDOW *wnp;
CUST *cp;
uchar back_att = (LIGHTGRAY << 4) | BLACK,
bdr_att = (LIGHTGRAY << 4) | BLACK,
csr_att = (CYAN << 4) | YELLOW,
first_att = (LIGHTGRAY << 4) | RED;
wnp = &Window1; /* set local window pointer */
init_video(80, 25); /* init video for 80 x 25 screen */
init_clock(0x3333); /* init clock irq at 91 tics/sec */
wn_create(0, 0, V_cols-1, V_rows-1, NO_BDR, WN_NORMAL, &Desk_wn);
link_window(&Desk_wn);
/*------------------------ create the menu system ------------------------*/
Drop_mnps[0] = &Files_menu;
Drop_mnps[1] = &Edit_menu;
menu_create(0, 0, V_cols - 1, 0, M_HORIZONTAL,
back_att, bdr_att, csr_att, first_att,
NO_BDR, WN_NORMAL, Top_mnp);
item_add( " Files ", 1, 3, &Top_menu );
item_add( " Edit ", 2, 3, &Top_menu );
menu_create(0, 1, 14, 5, M_VERTICAL,
back_att, bdr_att, csr_att, first_att,
SGL_BDR, WN_NORMAL, Drop_mnps[0]);
item_add( " Load File", 4, 1, &Files_menu );
item_add( " Save File", 5, 1, &Files_menu );
item_add( " Quit ", 3, 3, &Files_menu );
menu_create(11, 1, 32, 4, M_VERTICAL,
back_att, bdr_att, csr_att, first_att,
SGL_BDR, WN_NORMAL, Drop_mnps[1]);
item_add( " Clear Current", 6, 7, &Edit_menu );
item_add( " Clear All ", 7, 7, &Edit_menu );
set_idle_func(disp_time); /* set background clock function */
wn_create(5, 5, 75, 20, SLD_BDR, WN_POPUP, wnp);
wn_color(YELLOW, BLUE, wnp); /* change the window colors */
wn_bdr_color(WHITE, BLUE, wnp); /* change the border's colors */
link_window(wnp);
/*------------- initialize first customer as EnQue Software --------------*/
cp = &Customers[0];
strcpy(cp->business, "EnQue Software");
strcpy(cp->name, "Kevin Huck & Boyd Gafford");
strcpy(cp->addr, "Rt. 1 Box 116C");
strcpy(cp->city, "Pleasant Hill");
strcpy(cp->state, "MO");
strcpy(cp->zip, "64080");
strcpy(cp->phone, "(816)987-2515");
strcpy(cp->fax, "(816)987-2515");
strcpy(cp->date, "09/11/92");
strcpy(cp->memo, "BBS 816-358-8990");
menu_set(Top_mnp);
while(!end_flag)
{
cp = &Customers[cust];
mv_cs(1,1, wnp);
wn_printf(wnp, "Customer:%3d", cust+1);
wn_plst(CENTERED, 3, "Use cursor keypad to select customer", &Desk_wn);
disp_cust(cp, wnp);
wait_event();
switch(Event.key)
{
/*------------------------- process menus ----------------------------*/
case KEY_ALT_Q: /* quit program */
end_flag = 1;
break;
case KEY_ALT_F: case KEY_ALT_E:
m_show();
if( Event.key == KEY_ALT_F )
ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'F',M_EXIT_ON_ESC);
else
ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'E',M_EXIT_ON_ESC);
switch( ret_val )
{
case 3: /* quit program */
end_flag = 1;
break;
case 4: /* load file */
file_load(Customers);
break;
case 5: /* save file */
file_save(Customers);
break;
case 6: /* clear one */
setmem(cp, sizeof(CUST), 0);
break;
case 7: /* clear all */
setmem(Customers, sizeof(Customers), 0);
break;
}
break;
/*---------------------- process cursor keys -------------------------*/
case KEY_HOME:
cust = 0;
break;
case KEY_END:
cust = MAX_CUST-1;
break;
case KEY_DN:
if( cust < MAX_CUST-1 )
cust++;
break;
case KEY_UP:
if( cust > 0 )
cust--;
break;
case KEY_PGUP:
if( cust >= 10 )
cust -= 10;
else
cust = 0;
break;
case KEY_PGDN:
if( cust < MAX_CUST-11 )
cust += 10;
else
cust = MAX_CUST-1;
break;
/*-------------------- process field edit keys -----------------------*/
case 'b': case 'B': /* get new business name */
mv_cs( 11, 3, wnp);
wn_gets_ll(cp->business, "________________________________",
"********************************",
swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
break;
case 'n': case 'N': /* get new contact name */
mv_cs( 11, 4, wnp);
wn_gets_ll(cp->name, "________________________________",
"********************************",
swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
break;
case 'a': case 'A': /* get new address */
mv_cs( 11, 5, wnp);
wn_gets_ll(cp->addr, "________________________________",
"********************************",
swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
break;
case 'c': case 'C': /* get new city */
mv_cs( 11, 6, wnp);
wn_gets_ll(cp->city, "________________________________",
"********************************",
swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
break;
case 's': case 'S': /* get new state */
mv_cs( 51, 6, wnp);
wn_gets_ll(cp->state, "__", "UU",
swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 2, wnp);
break;
case 'z': case 'Z': /* get new zip */
mv_cs( 60, 6, wnp);
wn_gets_ll(cp->zip, "_____", "#####",
swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 5, wnp);
break;
case 'p': case 'P': /* get new phone number */
mv_cs( 11, 7, wnp);
wn_gets_ll(cp->phone, "(___)___-____", " ### ### ####",
swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
break;
case 'f': case 'F': /* get new fax number */
mv_cs( 11, 8, wnp);
wn_gets_ll(cp->fax, "(___)___-____", " ### ### ####",
swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
break;
case 'd': case 'D': /* get new date */
mv_cs( 11, 9, wnp);
wn_gets_ll(cp->date, "__/__/__", "## ## ##", swap_nibbles(wnp->att),
G_EXIT_ON_FILL, 8, wnp);
break;
case 'm': case 'M': /* get new memo field */
mv_cs( 11, 10, wnp);
wn_gets_ll(cp->memo, "________________________________",
"********************************",
swap_nibbles(wnp->att), G_STRIP_END, 32, wnp);
break;
}
}
unlink_window(wnp); /* remove the window from screen */
wn_destroy(wnp);
set_idle_func(NULL); /* remove background function */
unlink_window(&Desk_wn); /* remove the window from screen */
wn_destroy(&Desk_wn);
end_clock();
end_video(); /* clean up before we exit */
return(1);
}
/*** end of main ***/
/**************/
/* ~disp_time */
/* ***************************************************************/
/* This routine is called in the background by wait_event and will display */
/* the time once per second. Notice the use of the global variables */
/* Uw_timers. There is an array of four "countdown" timers that are user */
/* accessible. Each "timer tic" will decrement the counts by one, until */
/* 0 is reached. By "reloading" the timer with "Tics_per_sec", we only */
/* display the time of day once per second. "Tics_per_sec" is set */
/* by init_clock. */
/****************************************************************************/
int disp_time(void)
{
time_t t;
if( !Uw_timers[0] ) /* has one second passed? */
{
Uw_timers[0] = Tics_per_sec; /* if so, reload timer */
t = time(NULL); /* get time */
mv_cs(55, V_rows-1, &Desk_wn); /* move window cursor */
wn_st_qty(ctime(&t), 24, &Desk_wn); /* output 24 characters */
return(1);
}
return(0);
}
/*** end of disp_time ***/
/**************/
/* ~disp_cust */
/* ***************************************************************/
/* This routine displays a customer in the desired window... */
/****************************************************************************/
void disp_cust(CUST *cp, WINDOW *wnp)
{
int r = 3;
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Business: %-32s", cp->business);
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Name : %-32s", cp->name );
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Address : %-32s", cp->addr );
mv_cs( 1, r++, wnp );
wn_printf( wnp, "City : %-32s State: %2s Zip: %5s",
cp->city, cp->state, cp->zip );
wn_cleol(wnp); /* clear to end of line */
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Phone : %-16s", cp->phone );
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Fax : %-16s", cp->fax );
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Date : %-10s", cp->date );
mv_cs( 1, r++, wnp );
wn_printf( wnp, "Memo : %-32s", cp->memo );
}
/*** end of disp_cust ***/
/**************/
/* ~file_load */
/* ***************************************************************/
/* This routine loads a file from disk into the customer array... */
/****************************************************************************/
int file_load(CUST *customers)
{
FILE *fp;
if( get_fname(Fname) )
{
if( (fp = fopen(Fname, "rb")) != NULL )
{
fread(customers, sizeof(CUST), MAX_CUST, fp);
fclose(fp);
tone(1024,10);
return(1);
}
}
return(0);
}
/*** end of file_load ***/
/**************/
/* ~file_save */
/* ***************************************************************/
/* This routine saves a file to disk from the customer array... */
/****************************************************************************/
int file_save(CUST *customers)
{
FILE *fp;
if( get_fname(Fname) )
{
if( (fp = fopen(Fname, "wb")) != NULL )
{
fwrite(customers, sizeof(CUST), MAX_CUST, fp);
fclose(fp);
tone(1024,10);
return(1);
}
}
return(0);
}
/*** end of file_load ***/
/**************/
/* ~get_fname */
/* ***************************************************************/
/* This routine prompts the user for a filename using a popup window... */
/****************************************************************************/
int get_fname(char *fname)
{
int ret_val = 1;
WINDOW wn;
wn_create(20, 8, 60, 10, SLD_BDR, WN_POPUP, &wn);
wn_set(&wn);
wn_plst(4, 0, "Enter filename:", &wn);
if( wn_gets_ll(fname, "____________", "************",
swap_nibbles(wn.att), G_STRIP_END, 12, &wn) == KEY_ESC )
ret_val = 0;
wn_destroy(&wn);
return(ret_val);
}
/*** end of get_fname ***/
/*** END OF FILE ***/